//implement BoostThreadImpl
//andycai_sc 2011-04-29

#include "GParallel/GBoost/BoostThreadImpl.h"

BoostThreadImpl::BoostThreadImpl(GThread* _thread) : GThreadImpl(_thread)
{
    m_booThread = 0;
}

BoostThreadImpl::~BoostThreadImpl()
{
    if (m_booThread != 0)
        delete m_booThread;
}

void BoostThreadImpl::startImpl()
{
    if (m_booThread != 0)
        delete m_booThread;
    Callable call(this);
    m_booThread = new boost::thread(call);
}

void BoostThreadImpl::stopImpl()
{
    m_booThread->interrupt();
}

void BoostThreadImpl::joinImpl()
{
    m_booThread->join();
}

void BoostThreadImpl::sleepImpl(long mseconds)
{
    boost::this_thread::sleep(boost::posix_time::milliseconds(mseconds));
}

BoostThreadImpl::Callable::Callable(GThreadImpl* threadImp) : m_threadImp(threadImp)
{
}

BoostThreadImpl::Callable::~Callable()
{
}

void BoostThreadImpl::Callable::operator()()
{
    m_threadImp->exec();
}

GThreadImpl* createThreadFromBoost(GThread *_thread)
{
	return new BoostThreadImpl(_thread);
}
